package com.timothy.millionaire;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.timothy.calculate.CalculatorUtil;
import com.timothy.calculate.NumberUtil;

public class Calculate2Activity extends BaseActivity {
	private static final String LOG_TAG = "HomeActivity";
	
	Context mContext;
	
	Spinner spinner1;
	ArrayAdapter adapter1;
    String[] years;
	int yearTarget;
    EditText cus_YearTarget;

	Spinner spinner2;
	ArrayAdapter adapter2;
    String[] returns;
	double yearlyReturn = 0.0;
    EditText cus_yearReturn;

    TextView result;
	Button calculate;

    TableRow adRow;
    AdView adView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        //hide title bar
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.calculate2);

        mContext = getApplicationContext();

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        years = this.getResources().getStringArray(R.array.year_target_array);
        adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, years);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(new SpinnerSelectedListener1());
        spinner1.setVisibility(View.VISIBLE);

        cus_YearTarget = (EditText) findViewById(R.id.cus_TargetYear);

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        returns = this.getResources().getStringArray(R.array.year_return_array);
        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, returns);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new SpinnerSelectedListener2());
        spinner2.setVisibility(View.VISIBLE);

        cus_yearReturn = (EditText) findViewById(R.id.cus_yearReturn);

        result = (TextView) this.findViewById(R.id.result);

        calculate = (Button) this.findViewById(R.id.calculate);
        calculate.setOnClickListener(calculateListener);

        adRow = (TableRow) this.findViewById(R.id.adRow);
        adView = new AdView(this, AdSize.BANNER, "a151b693eb6195b");
        adRow.addView(adView);
        adView.loadAd(new AdRequest());

	}
	
	class SpinnerSelectedListener1 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener1.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));
            if(arg2 == 10){
                cus_YearTarget.setVisibility(View.VISIBLE);
            }else{
                cus_YearTarget.setVisibility(View.INVISIBLE);
                yearTarget = Integer.valueOf(years[arg2]);
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}

	class SpinnerSelectedListener2 implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			Log.i(LOG_TAG,"SpinnerSelectedListener2.onItemSelected()...");
			Log.i(LOG_TAG,"arg2="+String.valueOf(arg2));
            if(arg2 == 10){
                cus_yearReturn.setVisibility(View.VISIBLE);
            }else{
                cus_yearReturn.setVisibility(View.INVISIBLE);
                String yearlyReturnStr = returns[arg2].replace("%", "");
                yearlyReturn = 0.01 * Double.valueOf(yearlyReturnStr);
            }
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			Log.i(LOG_TAG,"onNothingSelected()...");
		}
	}
	
	View.OnClickListener calculateListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
//        	CalculatorUtil cal = new CalculatorUtil();
            if(cus_YearTarget.getVisibility() == View.VISIBLE){

                if(TextUtils.isEmpty(cus_YearTarget.getText())){
                    ShowDialog("Warning","Year target must not be empty!");
                    return;
                }
                yearTarget = Integer.valueOf(String.valueOf(cus_YearTarget.getText()));
                if(yearTarget < 10 ){
                    ShowDialog("Warning","Year target must be greater than 10!");
                    return;
                }
                if( yearTarget > 100){
                    ShowDialog("Warning","Year target must be less than 100!");
                    return;
                }
            }

            if(cus_yearReturn.getVisibility() == View.VISIBLE ){

                if(TextUtils.isEmpty(cus_yearReturn.getText())){
                    ShowDialog("Warning","Year return must not be empty!");
                    return;
                }
                yearlyReturn = 0.01 * Double.valueOf(String.valueOf(cus_yearReturn.getText()));
                if(yearlyReturn < 0.1){
                    ShowDialog("Warning","Year return must be greater than 10%");
                    return;
                }
                if( yearlyReturn > 1){
                    ShowDialog("Warning","Year target must be less than 100%");
                    return;
                }
            }

            double yearInvest = CalculatorUtil.getYearInvest(yearTarget, yearlyReturn);
            result.setVisibility(View.VISIBLE);
            result.setText(NumberUtil.formatData2Deci(yearInvest));
        }
	};

    @Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
	
/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}*/

}
